<?php
	/*
	 * Questions_model
	 * @author: Tran Van Thanh
	 * Email  : thanhtran@vietnambiz.com
	 * Date:	01/09/2011
	 */
class Questions_model extends MY_Model{

	function __construct()
	{
		parent::__construct();		
	}
	
	function get_all_questions($limit = 1, $offset = 1)
	{
		$where = array(			
    		'a.status'	=> 'yes'
    	);		
        
        $q      =       $this->db->select('a.*,b.fullname')
                             ->from("questions as a")                 
                 			 ->join('customers as b','a.id_customer = b.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("questions as a")                             
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result; 
	}
	
	function get_questions_usually()
	{
		$where = array(
			'a.usually'	=> 'yes',
    		'a.status'	=> 'yes'
    	);    
        
        $this->db->select("a.*,b.fullname")
                 ->from("questions as a")                 
                  ->join('customers as b','a.id_customer = b.id')
                  ->where($where)                 			                     
                  ->limit(5)
                  ->order_by('a.date_create DESC'); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result;
	}
	
	function check_email_customers($email)
    {
    	$q  =   $this->db->select()->from("customers")->where('email',$email)->get();
        if($q->row())
        {
        	return true;
        }
        else
        {
        	return false;
		}
	} 
	
	function get_customers_info_by_email($email="")
	{
		$query = $this->db->where('email', $email)->get("customers");
        if($query->num_rows() > 0) {
            $data = $query->row();
        }
        return $data;
	}
	
	function get_answer_by_id_questions($id_questions = null)
	{		
		$where = array(
			'b.id'		=> $id_questions,
			'a.status'	=> 'yes'
		);
		
		$q = $this->db->select("a.*")
					 ->from("answers as a")
					 ->join("questions as b","a.id_question = b.id")
					 ->where($where)
					 ->get();		
        $result = $q->result();
        $q->free_result();        
        return $result;
	}

	function get_questions_detail($id_questions = null)
	{
		$where = array(
			'a.id'		=> $id_questions,
			'a.status'	=> 'yes'
		);
		$q = $this->db->select("a.*,b.fullname")
					 ->from("questions as a")
					 ->join("customers as b","a.id_customer = b.id")					 
					 ->where($where)
					 ->get();		
        $result = $q->result();
        $q->free_result();        
        return $result;
	}
	
	function search_questions($keyword = "", $limit = 10, $offset = 1)
	{	                            
		$q = $this->db->select('a.*,b.fullname')
                    ->from("questions as a")
                    ->join('customers as b','a.id_customer = b.id')
                    ->where('a.status','yes')
                    ->like('a.title',$keyword)
                    ->or_like('a.contents',$keyword)                    
                    ->order_by('a.date_create DESC')
                    ->limit($limit,$offset)
                    ->get();
        $rows = $q->result();
        $q->free_result();
        $result['rows'] = $rows;
            
        $q = $this->db->select('count(id) as cnt')
                    ->from("questions")
                    ->where('status','yes')
                    ->like('title',$keyword)
                    ->or_like('contents',$keyword)
                    ->get();
        $num_rows = $q->row()->cnt;
        $result['num_rows'] = $num_rows;
        return $result;
	}
}